﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.WindowsDesktopEx.WPF
{
    /// <summary>
    /// bool值同BooleanToVisibilityConverter相反的转换器
    /// false:显示;true:隐藏
    /// </summary>
    public class BooleanToVisibilityConverter2 : ValueConverterAbs
    {
        /// <summary>
        /// 构造函数初始化
        /// </summary>
        public BooleanToVisibilityConverter2()
            : base()
        {

        }

        /// <summary>
        /// 内存对象中转换到显示
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return Visibility.Collapsed;
            }

            bool visible = System.Convert.ToBoolean(value);
            if (visible)
            {
                return Visibility.Collapsed;
            }
            else
            {
                return Visibility.Visible;
            }
        }

        /// <summary>
        /// 显示转换回内存对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return true;
            }

            try
            {
                var visible = (Visibility)value;
                if (visible == Visibility.Visible)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                ZLoger.Error(ex);
                return true;
            }
        }
    }
}
